<?php 
 
function project_view($project) { 
  $content = array(); 
  // Add necessary CSS files...
  drupal_add_css('modules/image/image.css'); 
  drupal_add_css('modules/project/project.css'); 
  // Set the title
  drupal_set_title($project->name); 
  
  // Size and generate image thumbnail
  if(isset($project->image) && $project->image != '') {
    list($width, $height) = getimagesize(realpath($project->image)); 
    $wh = 'width: '.$width.'px; height: '.$height.'px;';
    $content['image'] = array( 
      '#value' => '<div class="image project-image" style="'.$wh.'"><img src="'.file_create_url($project->image).'" alt="" /></div>', 
      '#weight' => -10, 
    );
  }
  // Generate status from integer value in tables
  switch(intval($project->status)) { 
    case 0: 
      $status = false; 
      break; 
    case 1: 
      $status = 'in progress'; 
      break; 
    case 2: 
      $status = 'ended'; 
      break; 
  } 
   
  $content['summary'] = array( 
    '#value' => '<div class="project-summary"><em>'.html_entity_decode($project->summary).'</em><ul>' 
                .'<li>Started: '.$project->started.'</li>' 
                . ((!$status) ? '' : '<li>Status: '.$status.'</li>') 
                .'</div>', 
    '#weight' => -5, 
    '#suffix' => '<div class="clear-line"></div>' 
  ); 
   
  $content['page'] = array( 
    '#value' => $project->page, 
    '#weight' => 5, 
  ); 
   
  $content['publications'] = array( 
    '#value' => '<h2>Publications</h2>', 
    '#weight' => 10, 
    '#prefix' => '<ol>', 
    '#suffix' => '</ol>', 
  ); 
   
  $publications = db_query("SELECT * FROM {project_publications} WHERE {project_publications}.project_id='%d'", $project->pid); 
  while($project_publication = db_fetch_object($publications)) { 
    $publication = publication_load($project_publication->publication_id);
    // Set "In . . . " phrase based on type of publication
    if($publication->type == 'article') { 
      $publication_title = 'In "'.$publication->bibtex_journal.'", '; 
    } else if($publication->type == 'inbook') { 
      $publication_title = 'In ".'.$publication->bibtex_booktitle.'", '; 
    } else if($publication->type == 'techreport') { 
      $publication_title = 'Technical report '.$publication->bibtex_number.', '; 
    } else { 
      $publication_title = ''; 
    } 
    $publisher = $publication->bibtex_publisher; 
    
    // Add ThickBox stuff
    drupal_add_css("modules/thickbox/thickbox.css"); 
    drupal_add_js("modules/thickbox/thickbox.js"); 
    
    // Create link to BibTex window
    $bibtex_link = '(<a title="BibTex" class="thickbox" href="#TB_inline?height=300&width=600&inlineId=bibtex_pub_'.$publication->pid.'">BibTex</a>)<div style="display:none" id="bibtex_pub_'.$publication->pid.'"><pre>'.publication_bibtex_output($publication).'</pre></div>'; 
    
    $string = '<li><strong>'.l($publication->bibtex_title, 'publication/'.$publication->pid).'</strong> | '.author_string($publication->bibtex_author, true).' '. 
                      $publication_title. 
                      $publication_pages. 
                      (($publication->bibtex_month != '')? $publication->month:''). 
                      $publication->bibtex_year.'. '.$bibtex_link.'</li>'; 
    $content['publications'][] = array( 
      '#value' => $string,
      '#weight' => -$publication->bibtex_year,
    ); 
  }
  // Build project members list
  $content['members'] = array( 
    '#value' => '<h2>Project members</h2>', 
    '#weight' => 9, 
    '#prefix' => '<ul>', 
    '#suffix' => '</ul>', 
  ); 
  $users = db_query("SELECT * FROM {people},{project_users} WHERE {people}.uid={project_users}.uid AND {project_users}.pid='%d' ORDER BY lastname", $project->pid); 
  while($person = db_fetch_object($users)) { 
    $content['members'][] = array( 
      '#value' => '<li>'.l((($person->title != '') ? $person->title.' ' : ucfirst($person->firstname)) . ' ' . ucfirst($person->lastname), 'user/'. $person->uid, array('attributes' => array('title' => t('View user profile.')))).'</li>', 
    ); 
  } 
  
  $content['nodes'] = array( 
    '#value' => '<h2>Project pages</h2>', 
    '#weight' => 9, 
    '#prefix' => '<ul>', 
    '#suffix' => '</ul>', 
  );
  $content['images'] = array( 
    '#value' => '<h2>Project images</h2>', 
    '#weight' => 9, 
    '#prefix' => '<div>', 
    '#suffix' => '<div style="clear: both;"></div></div>', 
  );
  $content['news'] = array(
    '#value' => '<h2>Project news</h2>',
    '#weight' => 9, 
    '#prefix' => '<ul>', 
    '#suffix' => '</ul>', 
  );
  // Build project nodes list
  $nodes = db_query("SELECT * FROM {node},{node_revisions},{project_nodes} WHERE {node}.nid={project_nodes}.nid AND {node}.vid={node_revisions}.vid AND {project_nodes}.pid='%d'", $project->pid); 
  while($node = db_fetch_object($nodes)) {
    if($node->type == 'image') {
      $image = db_fetch_object(db_query("SELECT * FROM {assets} WHERE vid='%d'", $node->vid));
      $content['images'][] = array(
        '#value' => '<div class="gallery-thumbnail-container" style="float: left;">'.l('<img class="gallery-thumbnail" src="'.$GLOBALS['base_url'].'/'.file_directory_path().'/'.variable_get('image_thumbnail_dir', 'thumbnails').'/'.$image->thumbnail.'" alt="" />', 'node/'.$node->nid, array('html' => true)).'</div>', 
      );
    } else if($node->type == 'story') {
      $content['news'][] = array( 
        '#value' => '<li>'.l($node->title , 'node/'.$node->nid). ' ('.date('M. d, Y', $node->changed).')'.'</li>', 
      ); 
    } else {
      $content['nodes'][] = array( 
        '#value' => '<li>'.l($node->title, 'node/'.$node->nid).'</li>', 
      ); 
    }
  }
  // Delete sections that don't have any content
  if(count($content['nodes']) < 5) {
    unset($content['nodes']);
  }
  if(count($content['news']) < 5) {
    unset($content['news']);
  }
  if(count($content['images']) < 5) {
    unset($content['images']);
  }
  if(count($content['members']) < 5) {
    unset($content['members']);
  }
  if(count($content['publications']) < 5) {
    unset($content['publications']);
  }
  
  return drupal_render($content); 
} 
 
function project_edit($project = null) { 
  return drupal_get_form('project_edit_form', $project); 
} 
function project_edit_form($form, $project=null) { 
  $form = array(); 
   
  $form['#attributes'] = array('enctype' => 'multipart/form-data'); 
  $form['project'] = array( 
    '#type' => 'fieldset', 
    '#title' => t('Project information'), 
    '#collapsible' => false, 
  ); 
  if(isset($project)) { 
    $form['project']['owner'] = array( 
      '#type' => 'hidden', 
      '#value' => $project->owner, 
    ); 
    $form['project']['pid'] = array( 
      '#type' => 'hidden', 
      '#value' => $project->pid, 
    ); 
    $form['project']['current_image'] = array( 
      '#type' => 'hidden', 
      '#value' => $project->image, 
    ); 
    drupal_add_css('modules/image/image.css'); 
     
    list($width, $height) = getimagesize(realpath($project->image)); 
    $wh = 'width: '.$width.'px; height: '.$height.'px;'; 
    $form['project']['img']['image-view'] = array( 
      '#type' => 'markup', 
      '#value' => '<div class="image" style="'.$wh.'"><img src="'.$GLOBALS['base_url'].'/'.$project->image.'" alt="" /></div>', 
    ); 
  } 
   
  $form['project']['name'] = array( 
    '#type' => 'textfield', 
    '#title' => t('Project name'), 
    '#required' => true, 
    '#default_value' => (isset($project) ? $project->name : ''), 
    '#size' => 40, 
  ); 
   
  $form['project']['started'] = array( 
    '#type' => 'textfield', 
    '#title' => t('Started'), 
    '#required' => true, 
    '#default_value' => (isset($project) ? $project->started : ''), 
    '#size' => 10, 
  ); 
  $form['project']['status'] = array( 
    '#type' => 'select', 
    '#title' => t('Current status'), 
    '#required' => false, 
    '#options' => array(0 => '<none>', 1 => 'in progress', 2 => 'ended'), 
    '#default_value' => (isset($project) ? $project->status : ''), 
    '#size' => 10, 
  ); 
   
  $form['project']['summary'] = array( 
    '#type' => 'textarea', 
    '#title' => t('Summary'), 
    '#rows' => 5, 
    '#default_value' => (isset($project) ? html_entity_decode($project->summary) : ''), 
  ); 
   
  $form['project']['page'] = array( 
    '#type' => 'textarea', 
    '#title' => t('Page description'), 
    '#rows' => 15, 
    '#default_value' => (isset($project) ? html_entity_decode($project->page) : ''), 
  ); 
   
  $form['project']['filters'] = filter_form(); 
   
  $form['project']['img']['image'] = array( 
    '#type' => 'file', 
    '#title' => t('Project image'), 
  ); 
   
  $form['project']['members'] = array( 
    '#type' => 'fieldset', 
    '#title' => 'Project members', 
    '#collapsible' => true, 
    '#collapsed' => false, 
  ); 
  $form['project']['promote'] = array( 
    '#type' => 'checkbox', 
    '#title' => 'Promote this project', 
    '#return_value' => 1, 
    '#default_value' => ($project->promote != 0) ? true : false, 
    '#weight' => 20, 
  ); 
   
  $people = db_query("SELECT uid, CONCAT(firstname, ' ', lastname) AS name FROM {people}"); 
  while($person = db_fetch_object($people)) { 
    $form['project']['members'][$person->uid] = array( 
      '#type' => 'checkbox', 
      '#return_value' => $person->uid, 
      '#title' => $person->name, 
      '#name' => 'project_members['.$person->uid.']', 
    ); 
  } 
  if(isset($project) && $project != false) { 
    $prj_people = db_query("SELECT * FROM project_users WHERE pid='%d'", $project->pid); 
    while($row = db_fetch_object($prj_people)) { 
      if(array_key_exists($row->uid, $form['project']['members'])) { 
        $form['project']['members'][$row->uid]['#default_value'] = true; 
      } 
    } 
  } 
   
  $form['project']['publications'] = array( 
    '#type' => 'fieldset', 
    '#title' => 'Project publications', 
    '#collapsible' => true, 
    '#collapsed' => false, 
  ); 
 
  $publications = db_query("SELECT pid, title FROM {publication_citation}"); 
  while($pub = db_fetch_object($publications)) { 
    $form['project']['publications'][$pub->pid] = array( 
      '#type' => 'checkbox', 
      '#return_value' => $pub->pid, 
      '#title' => $pub->title, 
      '#name' => 'project_publications['.$pub->pid.']', 
    ); 
  }
  $form['project']['nodes'] = array(
    '#type' => 'fieldset',
    '#title' => 'Project nodes',
    '#collapsible' => true,
    '#collapsed' => false,
  );
  
  $nodes = db_query("SELECT nid, title, type FROM {node}");
  while($node = db_fetch_object($nodes)) {
    $form['project']['nodes'][$node->type][$node->nid] = array(
      '#type' => 'checkbox',
      '#return_value' => $node->nid,
      '#title' => $node->title,
      '#name' => 'project_nodes['.$node->nid.']',
    );
  }
  $prj_nodes = db_query("SELECT {project_nodes}.*, {node}.type FROM {project_nodes},{node} WHERE pid='%d' AND {project_nodes}.nid={node}.nid", $project->pid);
  while($row = db_fetch_object($prj_nodes)) { 
    if(array_key_exists($row->nid, $form['project']['nodes'][$row->type])) { 
      $form['project']['nodes'][$row->type][$row->nid]['#default_value'] = true; 
    }
  }
  
  foreach($form['project']['nodes'] as $key => $value) {
    if('#' != substr($key, 0, 1)) {
      $form['project']['nodes'][$key]['#title'] = ucfirst($key).' nodes';
      $form['project']['nodes'][$key]['#type'] = 'fieldset';
      $form['project']['nodes'][$key]['#collapsible'] = true;
      $form['project']['nodes'][$key]['#collapsed'] = true;
    }
  }
  
  if(isset($project) && $project != false) { 
    $prj_pub = db_query("SELECT * FROM {project_publications} WHERE project_id='%d'", $project->pid); 
    while($row = db_fetch_object($prj_pub)) { 
      if(array_key_exists($row->publication_id, $form['project']['publications'])) { 
        $form['project']['publications'][$row->publication_id]['#default_value'] = true; 
      } 
    } 
  } 
   
  $form['project']['submit'] = array( 
    '#type' => 'submit', 
    '#weight' => 1000, 
    '#value' => t('Save'), 
  ); 
  if(isset($project)) { 
    $form['project']['delete'] = array( 
      '#type' => 'markup', 
        '#weight' => 1000, 
      '#value' => l('Delete', 'project/'.$project->pid.'/delete', array('attributes' => array('style' => 'float: right; border: 1px solid #AAA; padding: 4px;'))), 
    ); 
  } 
   
  return $form; 
} 
function project_edit_form_validate($form, &$form_state) { 
  $users = $form_state['clicked_button']['#post']['project_members']; 
  if(count($users) > 0) 
  foreach($users as $key => $value) { 
    $value = intval($value); 
    if(!$value || $value < 0) unset($users[$key]); 
  } 
  $form_state['clicked_button']['#post']['project_members'] = $users; 
   
  $pub = $form_state['clicked_button']['#post']['project_publications']; 
  if(count($pub) > 0) 
  foreach($pub as $key => $value) { 
    $value = intval($value); 
    if(!$value || $value < 0) unset($pub[$key]); 
  } 
  $form_state['clicked_button']['#post']['project_publications'] = $pub; 
   
  $values = $form_state['values']; 
  $values['pid'] = intval($values['pid']); 
  $values['name'] = check_plain(substr($values['name'], 0, 255)); 
  $values['summary'] = check_plain(substr($values['summary'], 0, 500)); 
  $values['started'] = check_plain(substr($values['started'], 0, 64)); 
  $values['page'] = _filter_html($values['page'], $values['format']); 
   
  $values['status'] = intval($values['status']); 
   
  if(isset($_FILES['files']['name']['image']) && $_FILES['files']['name']['image'] != '') { 
    $upload = filemanager_upload_file('image', array('file_validate_is_image' => array())); 
    if(!$upload) form_set_error('image', t("Couldn't upload the file.")); 
    else { 
      $name = time().'_project_thumb.png'; 
      $path = file_directory_path().'/pictures/'.$name; 
      $rpath = realpath(file_directory_path()).'/pictures/'.$name; 
      $values['image'] = $path; 
      create_thumbnail(realpath($upload->filepath), substr($rpath, 0, strlen($rpath)-4), variable_get('project_image_size', 200)); 
    } 
  } else $values['image'] = $values['current_image']; 
  $form_state['values'] = $values; 
} 
 
function project_edit_form_submit($form, &$form_state) { 
  $values = $form_state['values']; 
  $title = $values['name']; 
  $summary = $values['summary']; 
  $started = $values['started']; 
  $page = $values['page']; 
  $image = $values['image']; 
  $promote = ($values['promote'] != 0) ? true : false; 
  $pid = $values['pid']; 
  $status = $values['status']; 
  if($pid == 0) { 
    db_query("INSERT INTO {project} (name, summary, page, image, owner, started, status, promote) VALUES('%s', '%s', '%s', '%s', '%d', '%d', '%d', '%d')", $title, $summary, $page, $image, $GLOBALS['user']->uid, $started, $status, $promote); 
  } else { 
    db_query("INSERT INTO {project} (pid, name, summary, page, image, owner, started, status, promote)". 
             "VALUES('%d', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%d')". 
             "ON DUPLICATE KEY UPDATE name='%s', summary='%s', page='%s', image='%s', started='%s', status='%d', promote='%d'",  
             $pid, $title, $summary, $page, $image, $GLOBALS['user']->uid, $started, $status, $promote, 
             $title, $summary, $page, $image, $started, $status, $promote); 
  } 
   
  $val = db_fetch_object(db_query("SELECT mlid FROM {menu_links} WHERE link_title='%s'", $title)); 
  if(!$val) { 
    if($pid == 0) $dbid = db_last_insert_id('project', 'pid'); 
    else $dbid = $pid; 
    $plid = db_fetch_object(db_query("SELECT mlid FROM {menu_links} WHERE link_path='project'")); 
    $plid = $plid->mlid; 
     
    $mitem = array(); 
    $mitem['plid'] = $plid; 
    $mitem['link_title'] = $title; 
    $mitem['link_path'] = 'project/'.$dbid; 
     
    menu_link_save($mitem); 
  } 
  $users = $form_state['clicked_button']['#post']['project_members']; 
  if(count($publications) > 0) 
  foreach($users as $key => $value) { 
    db_query("INSERT IGNORE INTO {project_users} (pid, uid) VALUES('%d', '%d')", (!$pid) ? $dbid : $pid, $value); 
  } 
  if($pid != 0) { 
    $current = db_query("SELECT * FROM {project_users} WHERE pid='%d'", $pid); 
    while($row = db_fetch_object($current)) { 
      if(!isset($users[$row->uid])) db_query("DELETE FROM {project_users} WHERE pid='%d' AND uid='%d'", $pid, $row->uid); 
    } 
  } 
 
  $publications = $form_state['clicked_button']['#post']['project_publications']; 
  if(count($publications) > 0) 
  foreach($publications as $key => $value) { 
    db_query("INSERT IGNORE INTO {project_publications} (project_id, publication_id) VALUES('%d', '%d')", (!$pid) ? $dbid : $pid, $value); 
  } 
  if($pid != 0) { 
    $current = db_query("SELECT * FROM {project_publications} WHERE project_id='%d'", $pid); 
    while($row = db_fetch_object($current)) { 
      if(!isset($publications[$row->publication_id])) db_query("DELETE FROM {project_publications} WHERE project_id='%d' AND publication_id='%d'", $pid, $row->publication_id); 
    } 
  }
  $nodes = $form_state['clicked_button']['#post']['project_nodes']; 
  if(count($nodes) > 0) 
  foreach($nodes as $key => $value) {
    if(intval($value) > 0)
    db_query("INSERT IGNORE INTO {project_nodes} (pid, nid) VALUES('%d', '%d')", (!$pid) ? $dbid : $pid, $value); 
  } 
  if($pid != 0) { 
    $current = db_query("SELECT * FROM {project_nodes} WHERE pid='%d'", $pid); 
    while($row = db_fetch_object($current)) { 
      if(!isset($nodes[$row->nid])) db_query("DELETE FROM {project_nodes} WHERE pid='%d' AND nid='%d'", $pid, $row->nid); 
    }
  }
  $form_state['redirect'] = 'project/'.$dbid; 
} 
 
function project_delete($form, &$form_state) { 
  $pid = intval($form_state['values']['projectid']); 
  db_query("DELETE FROM {project} WHERE pid='%d'", $pid); 
  db_query("DELETE FROM {project_users} WHERE pid='%d'", $pid); 
  db_query("DELETE FROM {project_publications} WHERE project_id='%d'", $pid); 
  db_query("DELETE FROM {project_assets} WHERE pid='%d'", $pid); 
  db_query("DELETE FROM {menu_links} WHERE link_path='%s'", 'project/'.$pid); 
  return 'Deletion successful.'; 
} 
 
function project_confirm_delete($form, $project) { 
  $form = array(); 
  $form['projectid'] = array( 
    '#type' => 'hidden', 
    '#value' => $project->pid, 
  ); 
  $form['confirm'] = array( 
    '#type' => 'submit', 
    '#value' => 'Confirm', 
    '#suffix' => l('Cancel', 'project/'.$project->pid), 
  ); 
  return $form; 
} 
 
function project_confirm_delete_submit($form, &$form_state) { 
  project_delete($form, $form_state); 
  drupal_set_message('Project has been deleted.'); 
  $form_state['redirect'] = 'project'; 
} 
 
